#pragma once
#include <tom.ice>

module Xts
{
	module Tom
	{
		module Trading
		{
			//Trading Data Type Definitions, used by trading Server and Trading Client
		   //Broker order state enumeration.
		   enum XOrderState{UnknownState, Created, PendingSubmitted, Submitted, PendingModify, PendingCancel, Cancelled, Modified, Accepted, Filled, Rejected, Invalid, PartiallyFilled, PartiallyCancelled, PartiallyModified};
		   //The order type (market, limit, stop, etc.) , The validity of a type depends on the broker.
		   enum XOrderType{UnknownOrderType, Market, MarketOnOpen, MarketOnClose, Limit, LimitOnOpen, LimitOnClose, PeggedToMarket, Stop, StopLimit, TrailingStop};
		   //Broker transaction type enumeration.  
		   enum XTransactionType{UnkownTrancType, OpenLong, OpenShort, OpenCovered, CloseLong, CloseShort, Interest, CloseLongToday, CloseShortToday, ForceCloseLong, ForceCloseShort};
		   //Trade type, used to keep track of the purpose of orders and trades.
		   enum XTradeType{UndefinedTradeType, UserSubmitted, OpenPosition, ClosePosition, ProfitTarget, StopLoss, TrailingStopTrade};
		   //Invest type, the behavior of an order
		   enum XInvestType{UndefinedInvest, Tradable, HeldToMaturity, AvailableSale, LoanRecievable, Speculation, Hedge, Arbitrage};
		   //Validity of an order
		   enum XValidityType{UndefinedValidity, IOC, GFS, GFD, GTD, GTC, GFA};
		   //Volume condition of an order
		   enum XVolumeType{AnyVolume, MinVolume, TotalVolume};
		   //Force Close Reason
		   enum XForceCloseReason{NotForceClose, LackDeposit, ClientOverPositionLimit, MemberOverPositionLimit, NotMultiple, Violation, OtherReason, CloseToDelivery};
		   
		   //A quote order which should be submitted to a broker
		   ["cpp:class", "cs:attribute:System.Serializable"] 
		   struct XBrokerOrder{	      
				string							CustomOrderId;								//Custom setting order id
				string							BrokerId;									//Broker's id
				string							BatchNo;									//Batch No. for Batch order		  
				string							TradingAccountId;							//trader's trading account id
				XSymbolTag						SymbolTag;									//Order ralated symbol's tag
				XAssetClass						SymbolAssetClass = UnkownAsset;				//Order ralated symbol's asset class
				XOrderType						OrderType = UnknownOrderType;				//Order's type
				XTransactionType				TransactionType = UnkownTrancType;			//Transaction's type
				XTradeType						TradingWay = UndefinedTradeType;			//Trading way
				XInvestType						InvestWay = UndefinedInvest;	  			//Invest way
				double							LimitPrice = 0;								//Limit price for limit order, 0 for market order
				long							Shares = 0;									//Shares of the order
				XValidityType					ValidityCondition = UndefinedValidity;	  	//Validity Condition of the order
				Util::XTime						GTDDate;	  								//GTD date
				double							StopPrice = 0;								//Stopping price, 0 means no stopping price.
				XVolumeType						TrancVolumeType = AnyVolume;				//Volume type of a fill
				long							MinShares = 0;	  							//Min shares of a fill
				XForceCloseReason				ForceCloseReason = NotForceClose;			//Forced Close Reason, only for forced close order
		   };
		   
		   //Respond an order's broker Id after broker recieved it
		   ["cpp:class", "cs:attribute:System.Serializable"] 
		   struct XOrderStatus{
				string             				CustomOrderId;								//Custom setting order id
				string             				BrokerOrderId;								//Broker's order id
				string             				BatchNo;									//Batch No. for Batch order		 
				string             				TradingAccountId;							//trader's trading account id
				XOrderState         			OrderState;									//Order's current state
				long               				VolumeTraded;								//Volume already traded
				long               				VolumeLeft;									//Volume not traded yet
				long               				VolumeCanceled;	   							//Volume canceled
				Util::XTime              		UpdateTime;									//Update Time
		   };
		   
		   //A Deal on an Order
		   ["cpp:class", "cs:attribute:System.Serializable"] 
		   struct XOrderFill{
				string              			TransactionId;								//Transaction id
				string              			BrokerOrderId;								//Broker's order id
				string              			CustomOrderId;								//Custom setting order id
				string              			BatchNo;									//Batch No. for Batch order		
				string              			TradingAccountId;							//trader's trading account id
				XTransactionType     			TransactionType = UnkownTrancType;			//Transaction type
				double              			Commission;     							//Commission of this fill
				Util::XTime               		FillDateTime;								//Time of the fill
				double              			Price;										//Price of this fill
				long                			Quantity;									//Quantity	of this fill
		   };
		   sequence<XOrderFill>  XOrderFillSeries;
		   
		   //An Order's Status
		   ["cpp:class", "cs:attribute:System.Serializable"] 
		   struct XOrder{
				XBrokerOrder        			SubmitedOrder;								//the submited broker order
				string             				SeatId;										//Seat id of the trading account
				XOrderState         			OrderState;									//Order's current state
				int                				OrderUpdateSerial;							//Serial of the updating
				string             				BrokerOrderId;								//Broker's order id
				long               				VolumeTraded;								//Volume traded
				double             				TurnOver;									//Turn over of this order
				long               				VolumeCanceled;								//Volume Canceled
				long               				VolumeLeft;									//Volume not traded yet
				Util::XTime              		SubmitTime;									//Submit Time of this order
				Util::XTime              		UpdateTime;									//Update Time of this order	
				string             				ErrMsg; 	  	   							//Error Message
		   };
		   
		   ["cpp:class", "cs:attribute:System.Serializable"] 
		   struct XCancelOrderInfo{
				string            				CustomOrderId;								//Custom order id of the order to be canceled
				string             				BrokerOrderId;								//Broker's order id of the order to be canceled
				string             				TradingAccountId;							//Trading account id
				XExchangeType       			ExchangeId = UnknownEx;						//Exchange's id
				string             				BatchNo;									//Batch No. for Batch order		
				string             				CustomCancelOrderId;						//Custom cancel order id
		   };
		   
		   ["cpp:class", "cs:attribute:System.Serializable"] 
		   struct XModifyOrderInfo{
				string							CustomOrderId;								//Custom order id of the order to be modified
				string             				BrokerOrderId;								//Broker's order id of the order to be modified
				string             				TradingAccountId;							//Trading account id
				XExchangeType       			ExchangeId = UnknownEx;						//Exchange's id
				string             				BatchNo;									//Batch No. for Batch order		
				string            		 		CustomModifyOrderId;						//Custom modify order id
				double             				LimitPrice;									//New limit price
				long               				VolumeChange;	   							//New Volume
		   };
		   
		   //Broker Order Delivery Server, Used to delivery broker order to counter
		   ["amd"] interface IXBrokerOrderDelivery{
				void SendOrder(XBrokerOrder anOrder);
				void CancelOrder(XCancelOrderInfo cancelInfo);
				void ModifyOrder(XModifyOrderInfo modifyInfo);
		   };
	   
		   //Client Order Notify, used by trading client to receive order respond from trading server
		   ["amd"] interface IXClientOrderNotify{
				void OrderStatusResponse(XOrderStatus orderStatusRsp);
				void OrderFillResponse(XOrderFill orderFillRsp);	  
		   };
		   
		   enum XPositionType{LongPos, ShortPos};
			
			["cpp:class", "cs:attribute:System.Serializable"] 
			struct XPosition
			{
				string							InvestorId;									//Investor Id
				string							TradingAccountId;							//Trading account id
				string							BrokerId;									//Broker's id
				XSymbolTag						SymbolTag;									//Symbol's tag
				XPositionType					PosType;									//Position type: long/short
				XInvestType						HedgeFlag;									//Hedge, Abitrage, Speculation, Market Maker
				double							UsedMargin;									//Current Margin of this position
				int								CurrentSize;								//Current Size of this position
				double							PositionCost;								//Position Cost
				double							YdPositionCost;								//Position Cost of yesterday
				double							FrozenMargin;								//
				int								FrozenPosition;								//
				int								FrozenClosing;								//
				double							FrozenPremium;								//
				Util::XCurrencyType				PosCurrency;								//
			};
			
			["cpp:class", "cs:attribute:System.Serializable"] 
			struct XTradingAccount
			{
				string							BrokerId;									//Broker's id
				string							InvestorId;									//Investor Id
				string							TradingAccountId;							//Trading account id
				double							PreBalance;									//
				double							Deposit;
				double							Withdraw;
				double							FrozenMargin;
				double							FrozenFee;
				double							FrozenPremium;								//
				double							Available;
				double							LongFrozenMargin;
				double							ShortFrozenMargin;
				double							LongMargin;
				double							ShortMargin;
				double							DynamicInterest;
				double							TodayDeposit;
				double							Margin;
				double							Premium;
				double							RiskDegree;
			};
		};
	};
};